<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SwtGood;
use App\Models\SwtMachineGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class SwtGoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SwtGood(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('goods_picture')->image('',60,60);
            $grid->column('bar_code')->copyable();
            $grid->column('goods_name');
            $grid->column('weight');
            $grid->column('cost_price') -> display( function ($value) {
                return '¥'.bcdiv($value, 1000, 3);
            });
            $grid->column('sell_price') -> display( function ($value) {
                return '¥'.bcdiv($value, 100, 2);
            });
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('sku_id');
                $filter->like('goods_name');
                $filter->like('bar_code');
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableQuickEdit(false);
            });

            $grid->quickSearch('goods_name');
        });
    }


    protected function form()
    {
        $bar_code = request('bar_code');

        return Form::make(new SwtGood(), function (Form $form) use ($bar_code) {
            $form -> disableResetButton();

            if($form -> isEditing()) {
                $form -> model() -> sell_price /= 100;
                $form -> model() -> cost_price /= 1000;
            }

            $is_create = $form -> isCreating();

            if($is_create) {
                $form -> hidden('sku_id') -> value($bar_code);
                $form -> hidden('bar_code') -> value($bar_code);
            }

            $is_edit = $form -> isEditing();

            $form -> row(function (Form\Row $form) use ($is_create, $bar_code){
                $form -> width(4) -> text('goods_name') -> rules('required|max:20')-> required();

                $form -> width(4) -> text('weight') -> rules('required|max:20')-> required();

                $form -> width(4) -> decimal('integral_num');
            });

            $form -> row(function (Form\Row $form) use ($is_edit) {
                $form -> width($is_edit ? 4 : 6)
                    -> currency('cost_price')
                    -> symbol('¥')
                    -> rules('required')
                    -> required()
                    -> saving(function ($value) {
                        return $value * 1000;
                    });
                $form -> width($is_edit ? 4 : 6)
                    -> currency('sell_price')
                    -> symbol('¥')
                    -> rules('required|gte:cost_price')
                    -> required()
                    -> saving(function ($value) {
                        return $value * 100;
                    });

                $is_edit && $form -> width(4) -> switch('async_goods','是否更新库存价格')->default(0);
            });
            $form -> row(function (Form\Row $form) {
                $form -> width()->image('goods_picture')->move('upload/goods/'.date('Ymd'))->uniqueName()->autoUpload();
            });

            $form -> saving( function (Form $form) {
                if($form -> isEditing()) {
                    if($form -> input('async_goods')) {
                        SwtMachineGood::where('goods_id', $form->getKey()) -> update(['sell_price' => $form->sell_price * 100]);
                    }
                    $form->deleteInput('async_goods');
                }

            });

            $form -> saved( function (Form $form, $result) {
                if($result) {
                    return $form->response()->success('操作成功')->redirect('swt_good');
                } else {
                    return $form->response()->error('操作失败');
                }
            });
        });
    }
}
